package com.example.seabedmusic.views;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.seabedmusic.R;
import com.example.seabedmusic.adapter.SettingsAdapter;
import com.example.seabedmusic.entity.SettingItem;
import java.util.ArrayList;
import java.util.List;

public class GeneralSettingsActivity extends AppCompatActivity implements SettingsAdapter.OnSettingItemClickListener {

    private RecyclerView rvGeneral;
    private SettingsAdapter adapter;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_list);

        preferences = getSharedPreferences("app_settings", MODE_PRIVATE);
        initViews();
        setupGeneralList();
    }

    private void initViews() {
        rvGeneral = findViewById(R.id.rvSettings);
        rvGeneral.setLayoutManager(new LinearLayoutManager(this));
        setTitle("通用设置");
    }

    private void setupGeneralList() {
        List<SettingItem> items = new ArrayList<>();

        items.add(new SettingItem("主题模式", R.drawable.ic_theme, SettingItem.TYPE_NORMAL));
//        items.add(new SettingItem("语言设置", R.drawable.ic_language, SettingItem.TYPE_NORMAL));
//        items.add(new SettingItem("播放设置", R.drawable.ic_play_settings, SettingItem.TYPE_NORMAL));
//        items.add(new SettingItem("下载设置", R.drawable.ic_download, SettingItem.TYPE_NORMAL));
//        items.add(new SettingItem("清除缓存", R.drawable.ic_clear_cache, SettingItem.TYPE_NORMAL));
        items.add(new SettingItem("数据同步", R.drawable.ic_sync, SettingItem.TYPE_NORMAL));

        adapter = new SettingsAdapter(items);
        adapter.setOnSettingItemClickListener(this);
        rvGeneral.setAdapter(adapter);
    }

    @Override
    public void onSettingItemClick(SettingItem item) {
        switch (item.getTitle()) {
            case "主题模式":
                showThemeDialog();
                break;
//            case "语言设置":
//                showLanguageDialog();
//                break;
            case "播放设置":
                navigateToPlaySettings();
                break;
            case "下载设置":
                navigateToDownloadSettings();
                break;
            case "清除缓存":
                clearCache();
                break;
            case "数据同步":
                syncData();
                break;
        }
    }

    private void showThemeDialog() {
        String[] themes = {"浅色模式", "深色模式", "跟随系统"};
        int currentTheme = preferences.getInt("theme_mode", 2);

        new android.app.AlertDialog.Builder(this)
                .setTitle("选择主题")
                .setSingleChoiceItems(themes, currentTheme, (dialog, which) -> {
                    switch (which) {
                        case 0:
                            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                            break;
                        case 1:
                            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                            break;
                        case 2:
                            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                            break;
                    }
                    preferences.edit().putInt("theme_mode", which).apply();
                    dialog.dismiss();
                    Toast.makeText(this, "主题已切换", Toast.LENGTH_SHORT).show();
                })
                .show();
    }

//    private void showLanguageDialog() {
//        String[] languages = {"简体中文", "English", "繁体中文"};
//
//        new android.app.AlertDialog.Builder(this)
//                .setTitle("选择语言")
//                .setItems(languages, (dialog, which) -> {
//                    Toast.makeText(this, "已切换至: " + languages[which], Toast.LENGTH_SHORT).show();
//                    // 实际项目中这里应该更改应用语言
//                })
//                .show();
//    }

    private void navigateToPlaySettings() {
//        Intent intent = new Intent(this, PlaySettingsActivity.class);
//        startActivity(intent);
    }

    private void navigateToDownloadSettings() {
//        Intent intent = new Intent(this, DownloadSettingsActivity.class);
//        startActivity(intent);
    }

    private void clearCache() {
        new android.app.AlertDialog.Builder(this)
                .setTitle("清除缓存")
                .setMessage("确定要清除所有缓存数据吗？")
                .setPositiveButton("清除", (dialog, which) -> {
                    // 模拟清除缓存
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Toast.makeText(this, "缓存已清除", Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void syncData() {
        Toast.makeText(this, "开始同步数据...", Toast.LENGTH_SHORT).show();
        // 模拟数据同步
        new android.os.Handler().postDelayed(() -> {
            Toast.makeText(this, "数据同步完成", Toast.LENGTH_SHORT).show();
        }, 2000);
    }
}